package com.zzw.service.impl;

import com.zzw.bean.*;
import com.zzw.dao.IClassDao;
import com.zzw.dao.IOrderFormDao;
import com.zzw.dao.ITeacherDao;
import com.zzw.dao.IUserDao;
import org.springframework.stereotype.Service;
import com.zzw.service.IClassService;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * Created by 张志伟 on 2017/11/17.
 */

@Service
public class ClassServiceImpl implements IClassService {
    @Resource
    private IClassDao classDao;
    @Resource
    private ITeacherDao teacherDao;
    @Resource
    private IUserDao userDao;
    @Resource
    private IOrderFormDao orderFormDao;


    public ArrayList<ClassIntro> GetAllClassIntroByGradeAndSubject(String grade, String subject) {

        return classDao.getAllClassIntroByGradeAndSubject(grade, subject);

    }

    public ArrayList<MyClassVo> getMyClassVoByTid(Integer tid) {


        //通过教师id 获取班级集合
        ArrayList<ClassInfo> classInfoList = classDao.getClassInfoListByTid(tid);

        //创建目标视图对象数组
        ArrayList<MyClassVo> myClassVoList = new ArrayList<MyClassVo>();

        //通过多次查询拼接数组
        if (!classInfoList.isEmpty()) {
            for (ClassInfo ci : classInfoList) {

                int cid = ci.getId();

                //通过课程id 获取订单集合
                ArrayList<OrderForm> orderFormList = orderFormDao.getOrderFormByCid(cid);
                //创建用户对象数组
                ArrayList<User> userList = new ArrayList<User>();

                if (!orderFormList.isEmpty()) {
                    for (OrderForm of : orderFormList) {
                        //遍历课程id下的所有订单，获取所有用户
                        int uid = of.getUserid();
                        User u = userDao.getUserByUid(uid);
                        userList.add(u);
                    }
                }
                MyClassVo mcv = new MyClassVo(ci,userList);
                myClassVoList.add(mcv);
            }
        }

        return myClassVoList;
    }

    public ClassDetailsVo GetClassDetailByCid(int cid) {

        ClassInfo ci = classDao.getClassInfoByCid(cid);
        ClassDetailsVo cdv = null;
        if (ci != null) {
            int tid = ci.getTeacherid();
            Teacher t = teacherDao.getTeacherByTid(tid);
            cdv = new ClassDetailsVo(t, ci);
        }

        return cdv;

    }


}
